<template>
  <div class="loginhome">
    <!-- 头部 -->
    <div class="loginheader">
      <span
        class="el-icon-caret-left"
        @click="goback"
        style="cursor: pointer"
      ></span>
      <span>播放界面</span>
    </div>
    <div class="divsty">
      <div class="name">{{ newArr[index].name }}</div>
      <div class="imgsty">
        <img :src="newArr[this.index].pic" class="picsty" />
      </div>
      <div class="audiosty">
        <span v-for="(item, index) in songurl" :key="index">
          <audio :src="item.url" controls autoplay></audio>
        </span>
        <div class="btnsty">
          <el-button type="primary" @click="previous">上一首</el-button>
          <el-button type="primary" @click="next">下一首</el-button>
          <el-button type="primary" @click="goremark">看评论</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newArr: [],
      index: 0,
      songurl: [],
    };
  },
  created() {
    this.getfm();
  },
  methods: {
    // 从首页私人fm来
    getfm() {
      this.axios
        .get(`/personal_fm`)
        .then((res) => {
          this.fm = res.data.data;
          this.newArr = this.fm.map((item) => {
            return {
              id: item.id,
              name: item.name,
              pic: item.album.picUrl,
            };
          });
          this.getsongurl();
        })
        .catch((err) => {
          console.log(err);
        });
    },

    // 固定功能

    // 获取播放歌曲的url
    getsongurl() {
      this.axios
        .get(`/song/url?id=${this.newArr[this.index].id}`)
        .then((res) => {
          this.songurl = res.data.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 看评论
    goremark() {
      this.$router.push({
        path: "/playsongremark",
        query: { id: this.newArr[this.index].id },
      });
    },
    // 上一首
    previous() {
      this.index--;
      this.getfm();
    },

    next() {
      this.index++;
      this.getfm();
    },

    // 返回上一页
    goback() {
      this.$router.go(-1);
    },
  },
};
</script>

<style scoped>
.loginheader {
  font-size: 18px;
  font-weight: 700;
  text-align: center;
  color: red;
  border-bottom: red 1px solid;
  height: 30px;
}
.picsty {
  width: 300px;
  height: 300px;
}
.divsty {
  text-align: center;
  margin-top: 10px;
}
.name {
  text-align: center;
  margin-top: 10px;
}
.imgsty {
  text-align: center;
  margin-top: 10px;
}
.audiosty {
  margin-top: 10px;
}
.btnsty {
  margin-top: 10px;
}
</style>
